﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace DesignPractise.CardCheck
{
    /// <summary>
    /// 应答码枚举
    /// </summary>
    public enum ResponseCodeEnum
    {
        查询信息一致 = 00,
        查询信息不一致 = 01,
        验证失败 = 99,

        银行卡号格式不合法 = 11,
        姓名格式不合法 = 12,
        手机号格式不合法 = 13,
        证件号格式不合法 = 14,

        商户ID或密码未提供 = 21,
        意外的验证信息=31,
    }
    /// <summary>
    /// 银行账号验证返回消息
    /// </summary>
    public record CardVeriMessage
    {
        readonly ResponseCodeEnum responseCode;
        readonly string responseText;
        public CardVeriMessage(ResponseCodeEnum responseCode, string responseText)
        {
            this.responseCode = responseCode;
            this.responseText = responseText;
        }
        public CardVeriMessage(string Json)
        {
            JObject tempJson=JObject.Parse(Json);
            try
            {
                responseCode = (ResponseCodeEnum)int.Parse(tempJson.Value<string>("responseCode"));
                responseText = tempJson.Value<string>("message");
            }catch{
                throw new CardVeriExeption(ResponseCodeEnum.意外的验证信息.ToString());
            }
        }
        public ResponseCodeEnum ResponseCode { get => responseCode; }
        public string ResponseText { get => responseText; }
        /// <summary>
        /// 将验证返回消息转化为布尔值
        /// </summary>
        /// <returns></returns>
        public bool AnalyseResonse() { return responseCode==ResponseCodeEnum.查询信息一致; }
    }

    public class CardVeriExeption : NullReferenceException
    {
        public CardVeriExeption(string message):base(message){}
    }
}
